import time

from modules.base import BaseModule
from modules.executors import thread_pool_executor as tp
from modules.executors import process_pool_executor as pp


class Storager(BaseModule):
    path = None

    def __init__(self):
        super(Storager, self).__init__()

    def set_file_save_path(self, path):
        self.path = path

    def process(self, item):
        index, task = item
        f = open(f'{self.path}/{index}.mp3', 'wb')
        f.write(task)
        f.close()

    def process_single_thread(self, tasks):
        for index, task in enumerate(tasks):
            self.process((index, task))

    def process_multi_thread(self, tasks):
        task_list = []
        for index, item in enumerate(tasks):
            task = tp.submit(self.process, (index, item))
            task_list.append(task)
        for task in task_list:
            task.result()

    def process_multi_process(self, tasks):
        task_list = []
        for index, item in enumerate(tasks):
            task = pp.submit(self.process, (index, item))
            task_list.append(task)
        for task in task_list:
            task.result()

    def process_coroutine(self, tasks):
        pass
